package com.xhwl.centiotdata.handler.device.impl;

import com.xhwl.centiotdata.handler.device.DeviceIntegrationFactory;
import com.xhwl.centiotdata.handler.device.DeviceIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.fushi.DeviceFuShiDTO;
import com.xhwl.centiotdata.pojo.dto.fushi.DeviceStatusFuShiDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.suppliersystem.fushi.DeviceFuShiHandler;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.pojo.cent.device.Device;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 18:07 2023/4/21
 */
@Slf4j
@Service
public class DeviceIntegrationFuShiHandler extends DeviceIntegrationHandler {

    @Autowired
    private IDeviceCentService deviceCentService;
    @Autowired
    private DeviceFuShiHandler deviceFuShiHandler;


    @Override
    public void afterPropertiesSet() throws Exception {
        DeviceIntegrationFactory.register(96, this);
    }

    @Override
    public void pullDevice(DeviceIntegrationConfigDTO configDTO) {
        if (configDTO.getDeviceType() == null) {
            log.error("projectId：{} pullDevice failed! deviceType is null!", configDTO.getProjectId());
            return;
        }
        switch (configDTO.getDeviceType()){
            //人行门禁设备
            case 1 :
                // 拉取数据
                List<DeviceFuShiDTO> deviceFuShiDTOList = deviceFuShiHandler.pullPedestrianDevice(configDTO, null);
                if (CollectionUtils.isEmpty(deviceFuShiDTOList)) {
                    return;
                }
                // 转换数据
                List<DeviceDTO> deviceDTOList = deviceFuShiHandler.handlePedestrianDevice(configDTO, deviceFuShiDTOList);
                // 存储业务中台
                deviceCentService.asyncUpdateDevice(deviceDTOList);
                break;
            default:
        }

    }

    @Override
    public void pullDeviceOnlineStatus(DeviceIntegrationConfigDTO configDTO) {
        if (configDTO.getDeviceType() == null) {
            log.error("projectId：{} pullDeviceOnlineStatus failed! deviceType is null!", configDTO.getProjectId());
            return;
        }
        switch (configDTO.getDeviceType()) {
            //人行门禁设备
            case 1:
                // 拉取数据
                List<DeviceStatusFuShiDTO> deviceStatusFuShiDTOList = deviceFuShiHandler.pullPedestrianDeviceOnlineStatus(configDTO);
                if (CollectionUtils.isEmpty(deviceStatusFuShiDTOList)) {
                    return;
                }
                // 转换数据
                List<Device> deviceList = deviceFuShiHandler.handlePedestrianDeviceOnlineStatus(configDTO, deviceStatusFuShiDTOList);
                // 存储业务中台
                deviceCentService.asyncUpdateOnlineStatus(deviceList);
                break;
            default:
        }
    }
}
